﻿
Public MustInherit Class bytesbase(Of T)
    Implements equalBytes, ICloneable, IComparable(Of bytesbase(Of T)), IComparable

    Protected d As T

    Public MustOverride Sub frombytes(ByVal b As bytes) Implements frombytes.frombytes
    Public MustOverride Function tobytes() As bytes Implements tobytes.tobytes

    Public Sub New()
        d = Nothing
    End Sub

    Public Sub New(ByVal i As T)
        [set](i)
    End Sub

    Public Sub [set](ByVal i As T)
        copy(d, i)
    End Sub

    Public Sub attach(ByVal i As T)
        d = i
    End Sub

    Public Property value() As T
        Get
            Return d
        End Get
        Set(value As T)
            [set](value)
        End Set
    End Property

    Public Shared Widening Operator CType(ByVal this As bytesbase(Of T)) As T
        Return +this
    End Operator

    Public Shared Operator +(ByVal this As bytesbase(Of T)) As T
        If this Is Nothing Then
            Return Nothing
        Else
            Return this.d
        End If
    End Operator

    Public NotOverridable Overrides Function ToString() As String
        Return Convert.ToString(d)
    End Function

    Public NotOverridable Overrides Function GetHashCode() As Int32
        If d Is Nothing Then
            Return 0
        Else
            Return d.GetHashCode()
        End If
    End Function

    Public NotOverridable Overrides Function Equals(ByVal obj As Object) As Boolean
        Return operatorEqual(Me, obj)
    End Function

    Public Function Clone() As Object Implements ICloneable.Clone
        Dim rtn As bytesbase(Of T) = Nothing
        rtn = alloc(Me)
        copy(rtn.d, d)
        Return rtn
    End Function

    Public Function CompareTo(ByVal obj As Object) As Int32 Implements IComparable.CompareTo
        Return CompareTo(convertor.cast(Of bytesbase(Of T))(obj))
    End Function

    Public Function CompareTo(ByVal other As bytesbase(Of T)) As Int32 _
                        Implements IComparable(Of bytesbase(Of T)).CompareTo
        If Me Is Nothing AndAlso other Is Nothing Then
            Return 0
        ElseIf Me Is Nothing Then
            Return -1
        ElseIf other Is Nothing Then
            Return 1
        Else
            Return compare(d, other.d)
        End If
    End Function
End Class

Public Class bytesbytes
    Inherits bytesbase(Of bytes)

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal i As bytes)
        MyBase.New(i)
    End Sub

    Public Overrides Sub frombytes(ByVal b As bytes)
        copy(d, b)
    End Sub

    Public Overrides Function tobytes() As bytes
        Return d
    End Function

    Public Overloads Shared Widening Operator CType(ByVal this As bytes) As bytesbytes
        Return New bytesbytes(this)
    End Operator
End Class

Public Class bytesBoolean
    Inherits bytesbase(Of Boolean)

    Public Const TRUE_VALUE As Byte = maxUInt8

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal i As Boolean)
        MyBase.New(i)
    End Sub

    Public Overrides Sub frombytes(ByVal b As bytes)
        d = (b.size() = 1 AndAlso b(0) = TRUE_VALUE)
    End Sub

    Public Overrides Function tobytes() As bytes
        Return New bytes(ifoper(d, TRUE_VALUE, Not TRUE_VALUE))
    End Function

    Public Overloads Shared Widening Operator CType(ByVal this As Boolean) As bytesBoolean
        Return New bytesBoolean(this)
    End Operator
End Class